package cn.wolfcode.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2020/5/6.
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //查询
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){
        System.out.println("是否拥有角色:"+SecurityUtils.getSubject().hasRole("HR_MGR"));
        System.out.println("是否拥有角色:"+SecurityUtils.getSubject().hasRole("admin"));
        System.out.println("是否拥有权限:"+SecurityUtils.getSubject().isPermitted("employee:delete"));



        model.addAttribute("pageInfo",departmentService.query(qo));
        return "department/list";
    }
    //编辑,跳转到新增或者修改的jsp
    //改为用模态框
//    @RequiredPermission(name="部门编辑",expression="department:input")
//    @RequestMapping("/input")
//    public String input(Model model,Long id){
//        if(id != null){
//            model.addAttribute("dept",departmentService.get(id));
//        }
//        return "department/input";
//    }
    //保存修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department dept){
        try {
            if(dept.getId() != null){
                departmentService.update(dept);
            }else {
                departmentService.save(dept);
            }
            return new JsonResult();
        } catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
//        return "redirect:/department/list.do";

    }
    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            if(id != null){
                departmentService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
//        return "redirect:/department/list.do";
    }

}
